#include <iostream>
#include <atomic>
#include <thread>

std::atomic<int> data(0);
std::atomic<bool> ready(false);

void consumer()
{
    while (!ready.load(std::memory_order_acquire))
    {
        std::this_thread::yield();  // 主动让出CPU资源给其他线程。它的作用是暗示调度器将当前线程置于就绪状态，以便其他线程有机会执行。
    }
    int value = data.load(std::memory_order_consume);
    std::cout << "Consumed data: " << value << std::endl;
}

void producer()
{
    data.store(42, std::memory_order_release);
    ready.store(true, std::memory_order_release);
}

int main()
{
    std::thread t1(consumer);
    std::thread t2(producer);
    t1.join();
    t2.join();

    return 0;
}